{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### ***Note: This notebook is featured in an [instructional video](https://www.youtube.com/watch?v=ZxKvIAAjCeU) on visualizing FITS files in `pywwt`. Follow the link to see its creation in action!***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Loading and manipulating FITS files with `pywwt`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import dependencies."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from astroquery.skyview import SkyView\n",
    "from pywwt.jupyter import WWTJupyterWidget"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Layer FITS images over existing all-sky surveys."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We download a FITS image of a galaxy in the infrared using `astroquery`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "size = 500\n",
    "img_list = SkyView.get_images(position='SN 2011FE',\n",
    "                              survey='2MASS-K', pixels=size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sn11 = img_list[0]\n",
    "sn11.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load the image in `pywwt`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wwt = WWTJupyterWidget()\n",
    "wwt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you're using JupyterLab, right-click to the side of the viewer after it loads and select \"Create New View for Output.\":\n",
    "\n",
    "![](../data/separate-pane-instructions.jpg)\n",
    "\n",
    "Examine the area around the image by clicking and dragging the viewer or by pressing 'I', 'J', 'K', and 'L' as directional keys. Pinch in/out or press 'Z'/'X' to zoom."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wwt.background = wwt.imagery.ir.twomass\n",
    "wwt.foreground_opacity = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img = wwt.layers.add_image_layer(sn11)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It's also possible to upload a local FITS image by providing the relative path to the file as the argument for `add_image_layer()` — see the additional content at the end of the notebook for an example."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Manipulate the image in `pywwt`.\n",
    "\n",
    "We test different values for each of the image layer's attributes. We end by choosing background layers that help visualize the galaxy in three wavelengths at once."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img.opacity = .4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img.opacity = .6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img.vmin = 360"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img.vmax = 370"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img.stretch = 'power'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img.stretch = 'log'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once we've adjusted the image to our liking, we can load visible and gamma-wavelength layers in the viewer via code..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wwt.background = wwt.imagery.visible.sdss\n",
    "wwt.foreground = wwt.imagery.gamma.fermi\n",
    "wwt.foreground_opacity = .7"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "...or interactively through a Jupyter widget. It includes a slider for foreground opacity and respective drop-downs for the background and foreground layers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wwt.layer_controls"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also continue to tinker with the FITS image layer through another interactive widget."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img.controls"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## *Additional content*: Load a local FITS image"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As stated earlier, local FITS files are also fair game for `add_image_layer()`. You need only provide the path; `pywwt` does the rest of the work to open it up. To provide a view of M101 from yet another wavelength, we'll use an image from the Swift Soft X-ray Survey."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img2 = wwt.layers.add_image_layer('../data/m101_swiftx.fits')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This file was also created with `astroquery` with the following code:\n",
    "\n",
    "```\n",
    "img_list2 = SkyView.get_images(position='SN 2011FE', survey='SwiftXRTCnt', pixels=140)`\n",
    "img_list2[0].writeto('data/m101_swift')\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This time, we'll leave the image manipulation process to you."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img2.controls"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can find out more about adding layers [in our documentation](https://pywwt.readthedocs.io/en/stable/layers.html) and contact us with any comments or questions at the [WorldWide Telescope Forum](https://wwt-forum.org/) or by opening an issue or pull request at [`pywwt`'s main GitHub repository](https://github.com/WorldWideTelescope/pywwt). Thank you for trying out this example notebook on working with FITS images in `pywwt`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Credits\n",
    "\n",
    "This notebook was prepared by O. Justin Otor."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
